package com.egao.common.manage.company;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.entity.CompanyQuestion;
import com.egao.common.module.company.entity.CompanyQuestionnaire;
import com.egao.common.module.company.entity.CompanyQuestionnaireOption;
import com.egao.common.module.company.service.CompanyQuestionService;
import com.egao.common.module.company.service.CompanyQuestionnaireOptionService;
import com.egao.common.module.company.service.CompanyQuestionnaireService;
import com.egao.common.module.company.service.CompanyService;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author lym
 * @date 2023/3/10 9:57
 */
@Api(tags = "单位问卷")
@RestController
@RequestMapping("${api}")
public class CompanyQuestionnaireController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private CompanyService companyService;
    @Resource
    private CompanyQuestionnaireService companyQuestionnaireService;
    @Resource
    private CompanyQuestionnaireOptionService companyQuestionnaireOptionService;
    @Resource
    private CompanyQuestionService companyQuestionService;

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "单位完成问卷", desc = "单位完成问卷", result = true)
    @ApiOperation(value = "单位完成问卷")
    @PostMapping("/company/questionnaire/save")
    @Transactional
    public JsonResult save(@RequestBody CompanyQuestionnaire companyQuestionnaire) {
        Company company = companyService.getByUserId(getLoginUserId());
        if (company == null) {
            throw new BusinessException("没有找到单位信息");
        }

        List<CompanyQuestionnaireOption> optionList = companyQuestionnaire.getOptionList();
        if (optionList == null || optionList.size() == 0) {
            throw new BusinessException("没有找到调查问卷选项");
        }

        companyQuestionnaire.setUserId(getLoginUserId());
        companyQuestionnaire.setCompanyId(company.getId());
        companyQuestionnaire.setMark("202303");
        if (!companyQuestionnaireService.save(companyQuestionnaire)) {
            throw new BusinessException("保存调查问卷失败");
        }

        optionList.forEach(d -> {
            d.setCompanyQuestionnaireId(companyQuestionnaire.getId());
        });
        if (!companyQuestionnaireOptionService.saveBatch(optionList)) {
            throw new BusinessException("保存调查问卷选项失败");
        }

        User user = getLoginUser();
        user.setCompanyQuestionnaire("0");
        if (!userService.updateById(user)) {
            throw new BusinessException("更新用户调查问卷完成状态失败");
        }

        return JsonResult.ok("提交成功");
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "单位完成问卷", desc = "单位完成问卷", result = true)
    @ApiOperation(value = "单位完成问卷")
    @PostMapping("/company/question/save")
    @Transactional
    public JsonResult save(@RequestBody CompanyQuestion companyQuestion) {
        Company company = companyService.getByUserId(getLoginUserId());
        if (company == null) {
            throw new BusinessException("没有找到单位信息");
        }

        companyQuestion.setCompanyId(getLoginUserId());
        if (!companyQuestionService.save(companyQuestion)) {
            throw new BusinessException("保存调查问卷失败");
        }

        User user = getLoginUser();
        user.setCompanyQuestionnaire("0");
        if (!userService.updateById(user)) {
            throw new BusinessException("更新用户调查问卷完成状态失败");
        }

        return JsonResult.ok("提交成功");
    }

}
